import React, { Component } from 'react';

import Header from '../../core/components/header';
import Table from '../../core/components/table';
import FormSelect from '../../core/components/form.select';
import { Get, PostString } from '../../core/utils/fetch';
import Dialog from '../../core/utils/dialog';
import {
    Link
} from 'react-router-dom';

class InvestList extends Component {

    static defaultProps = {
        selectDataList: [
            // { value: '', name: '全部' },
            // { value: 'SUBMITTING', name: '待提交' },
            // { value: 'AUDITING', name: '待审核' },
            { value: 'AUDIT_PASS', name: '审核通过' },
            { value: 'AUDIT_REJECT', name: '审核不通过' },
        ],
    };

    state = {
        searchData: {
            status: this.props.selectDataList[0].value,
        },
    };

    render() {
        let columns = [
            { title: '序号', name: 'id' },
            { title: '姓名', name: 'custName' },
            { title: '联系方式', name: 'phone' },
            { title: '投资产品', name: 'productId.name' },
            { title: '投资金额', name: 'investNum' },
            { title: '年化收益', name: 'rate' },
            { title: '认购日期', name: 'calRateBeginDate', type: 'date' },
            { title: '回购日期', name: 'calRateEndDate', type: 'date' },
            { title: '审核状态', render: this.renderAduit },
            { title: '上传人', name: 'creatorName' },
            { title: '上传时间', name: 'createTime', type: 'date' },
            { title: '操作', render: this.renderOpertion },
        ];
        let { searchData } = this.state;
        return (
            <div>
                <Header title="投资用户" subTitle="Invest Manage"></Header>
                <Table
                    ref="table"
                    title="用户投资列表"
                    url={`/spi/custInvest/list?status=${searchData.status}`}
                    paramsJson={false}
                    columns={columns}>
                    <div className="row">
                        <div className="col-lg-12">
                            <FormSelect
                                className="pull-left"
                                labelClassName="pull-left"
                                selectClassName="col-lg-3 left-select"
                                label="审核状态"
                                name="status"
                                dataItem={searchData}
                                dataList={this.props.selectDataList}>
                                <button type="button" className="btn btn-sm btn-success pull-left btn-float" onClick={this._onChange}>查询</button>
                            </FormSelect>
                        </div>
                    </div>
                </Table>
            </div>
        );
    }

    renderAduit = (dataItem) => {
        switch (dataItem.auditInvestStatus) {
            case "SUBMITTING":
                return "待提交";
            case "AUDITING":
                return "待审核";
            case "AUDIT_PASS":
                return "审核通过";
            case "AUDIT_REJECT":
                return "审核不通过";
            default:
                return `未知状态-${dataItem.auditInvestStatus}`
        }
    };

    renderOpertion = (dataItem) => {
        if (dataItem.auditInvestStatus === "AUDIT_REJECT") {
            return (
                <div>
                    <Link to={{ pathname: "/investment-edit", search: `?id=${dataItem.id}` }}>编辑</Link>
                    {'    '}
                    <a onClick={() => this._onAudit(dataItem.id)}>提交审核</a>
                    {'    '}
                    <a onClick={() => this._onDel(dataItem.id)}>删除</a>
                </div>
            );
        }
        else {
            return (
                <div>
                    <Link to={{ pathname: "/investment-edit", search: `?id=${dataItem.id}&only=true` }}>查看</Link>
                    {'    '}
                    {dataItem.auditInvestStatus === 'AUDIT_PASS' && global.userData.isAdmin() ? <a onClick={() => this._onDel(dataItem.id)}>删除</a> : null}
                </div>
            );
        }
    };

    _onDel = (id) => {
        Dialog.confirm('你确定要删除吗', () => {
            PostString(`/spi/custInvest/del/${id}`)
                .then(() => {
                    this._refresh();
                });
        });
    };

    _onAudit = (id) => {
        Dialog.confirm('你确定要提交审核吗', () => {
            PostString(`/spi/custInvest/submit/${id}`)
                .then(() => {
                    this._refresh();
                });
        });
    };

    _onChange = () => {
        let { searchData } = this.state;
        this.setState({ searchData });
    };

    _refresh = () => {
        this.refs.table.refresh();
    };
}

export default InvestList;